package com.liuzhilin.controller;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.liuzhilin.domain.Article;
import com.liuzhilin.domain.Manager;
import com.liuzhilin.service.ManagerService;
import com.liuzhilin.service.UserService;
import com.liuzhilin.util.Response;
import jakarta.servlet.ServletException;
import jakarta.servlet.annotation.WebServlet;
import jakarta.servlet.http.HttpServlet;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;
/*
* 管理员页面文章打印
* */
@WebServlet("/managerArticlePrint")
public class ManagerArticlePrint extends HttpServlet {
        protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
            // 设置响应类型为 JSON
            Response.setCorsHeaders(response);
            response.setCharacterEncoding("UTF-8");
            // 设置响应内容类型为 JSON
            response.setContentType("application/json");
            PrintWriter out = response.getWriter();
            // 从请求中获取用户名
            String username = request.getParameter("username");
            username = username.trim();
            String type = request.getParameter("type");
            List<Article> articles= null;
            //判断是否是超级管理员
            if (username.equals("superManager")){
                articles = UserService.getArticles("所有博客");
            }else{
                articles = ManagerService.getArticles(username,type);
            }
            // 将用户信息转换为 JSON 格式，并返回给客户端，ObjectMapper是Jackson提供的一个类，用于将Java对象转换为JSON字符串
            ObjectMapper objectMapper = new ObjectMapper();
            objectMapper.writeValue(out, articles);


        }
        @Override
        public void doOptions(HttpServletRequest request, HttpServletResponse response){
            Response.setCorsHeaders(response);
            response.setStatus(HttpServletResponse.SC_OK);

        }
}
